package com.kun.myvirtualapkdemo;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.os.Environment;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import com.didi.virtualapk.PluginManager;

import java.io.File;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ActivityCompat.requestPermissions(this,
                new String[]{Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.WRITE_EXTERNAL_STORAGE},
                200);
        loadPlugin(this);
    }
    public void goToPlugin(View v){
        if(v.getId() == R.id.btnGoToPlugin){
            final String pkg = "com.kun.pluginapp";
            if (PluginManager.getInstance(this).getLoadedPlugin(pkg) == null){
                Toast.makeText(this,"No Plugin",Toast.LENGTH_SHORT).show();
                return;
            }
            Intent i = new Intent();
            i.setClassName(this,"com.kun.pluginapp.TestActivity");
            startActivity(i);
        }
    }
    private void loadPlugin(Context base) {
        PluginManager pluginManager = PluginManager.getInstance(base);
        File apk = new File(Environment.getExternalStorageDirectory(),"plugin.apk");
        if (apk.exists()) {
            try {
                pluginManager.loadPlugin(apk);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }else {
            Toast.makeText(this,"No Plugin apk...",Toast.LENGTH_SHORT).show();
        }
    }
}
